package com.hgkj.web.listener;

import com.hgkj.common.async.AsyncManager;
import com.hgkj.common.async.factory.CommonAsyncTaskFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * Spring容器初始化完成监听器组件
 * 初始化 持久登陆token
 */
@Slf4j
@Component
public class DevTokenInitializer implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 确保只在根容器初始化完成后执行一次
        if (event.getApplicationContext().getParent() == null) {
            generateDevToken();
        }
    }

    private void generateDevToken() {
        AsyncManager.getInstance().execute(CommonAsyncTaskFactory.devOldToken());
    }
}